import { Notify } from 'vant'
import { createStore } from 'vuex'
import cn from '../assets/cn-flag-icon.jpg'
import se from '../assets/se-flag-icon.png'

interface StoreInfo {
  store_name: string;
  store_desc: string;
  store_distance: string;
  store_id: number
}

interface cartItems {
  goods_id: string;
  goods_num: number,
  total_price: number
}

const cart: {total_price: number; total_num: 0;payment_id: string;delivery_date: string; delivery_time: string;tax: number;pay_amount: number; tax_price: number; runing_price: number; store: StoreInfo | null, items: Array<cartItems>} = {
  total_price: 0, //商品总价
  total_num: 0, //数量
  tax: 0.05, //税点
  tax_price: 0, //税价
  pay_amount: 0, //支付总价
  runing_price: 0, //跑腿费
  items: [],
  store: null,
  delivery_date: '',
  delivery_time: '',
  payment_id: ''
}

export default createStore({
    state () {
      return {
        country: [
            {
                name: 'China',
                icon: cn,
                countryid: 1
            },
            {
                name: 'Sweden',
                icon: se,
                countryid: 2
            }
        ],
        cart: JSON.parse(JSON.stringify(cart))
      }
    },
    mutations: {
      //更新配送价格
      setStore(state: any, action: any){
        console.log(action.payload)
        state.cart.store = action.payload
        state.cart.runing_price = action.payload.store_price
        state.cart.pay_amount = (Number.parseFloat(state.cart.total_price) + Number.parseFloat(state.cart.tax_price) + Number.parseFloat(state.cart.runing_price)).toFixed(2)
      },
      //更新购物车
      setCart(state: any, action: any) {
        let index = state.cart.items.findIndex((val: cartItems) => {return val.goods_id == action.payload.data.goods_id})
        if(index == -1){
          state.cart.items.push({
            goods_num: action.payload.num,
            total_price: action.payload.data.goods_price,
            ...action.payload.data
          })
        }else{
          state.cart.items[index].goods_num += action.payload.num
          state.cart.items[index].total_price = Number.parseFloat((state.cart.items[index].goods_num * state.cart.items[index].goods_price).toFixed(2))
        }
        state.cart.total_num = state.cart.items.map((v: cartItems) => v.goods_num ).reduce((previousValue: number, currentValue: number) => previousValue + currentValue).toFixed(2)
        state.cart.total_price = state.cart.items.map((v: cartItems) => v.total_price ).reduce((previousValue: number, currentValue: number) => previousValue + currentValue).toFixed(2)
        state.cart.tax_price = (state.cart.tax * state.cart.total_price).toFixed(2)
        state.cart.pay_amount = (Number.parseFloat(state.cart.total_price) + Number.parseFloat(state.cart.tax_price) + Number.parseFloat(state.cart.runing_price)).toFixed(2)
        Notify({ type: 'success', message: '添加购物车成功' });
      },
      //移出购物车
      removeCart(state: any, action: any){
        let index = state.cart.items.findIndex((val: cartItems) => {return val.goods_id == action.payload})
        if(index != -1){
          state.cart.items.splice(index, 1)
        }
        Notify({ type: 'success', message: '购物车移除成功' });
      },
      initCart(state: any) {
        state.cart = JSON.parse(JSON.stringify(cart))
      }
    },
    actions: {
      addCart({commit, state}, data: cartItems){
        commit({
          type: 'setCart',
          payload: data
        })
      },
      removeCart({commit, state}, data: cartItems){
        commit({
          type: 'removeCart',
          payload: data
        })
      },
      setStore({commit, state}, data: cartItems){
        commit({
          type: 'setStore',
          payload: data
        })
      },
      initCart({commit, state}){
        commit({
          type: 'initCart'
        })
      }
    },
    getters: {
      getCart(state){
        return state.cart
      }
    }
})